home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / DrawPix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  11.8 KB  |  338 lines

  1. /*
  2.  * (c) Copyright 1995, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * Author: John Spitzer, SGI Applied Engineering
  36.  *
  37.  */
  38. #include <math.h>
  39. #include "DrawPix.h"
  40. #include "DrawPixX.h"
  41.  
  42. #undef offset
  43. #define offset(v) offsetof(DrawPixels,v)
  44.  
  45. static InfoItem DrawPixelsInfo[] = {
  46. #define INC_REASON INFO_ITEM_ARRAY
  47. #include "DrawPix.h"
  48. #undef INC_REASON
  49. };
  50. #include <malloc.h>
  51.  
  52. DrawPixelsPtr new_DrawPixels()
  53. {
  54.     DrawPixelsPtr this = (DrawPixelsPtr)malloc(sizeof(DrawPixels));
  55.     ImagePtr thisImage = (ImagePtr)(&this->image_DrawPixels);
  56.     TransferMapPtr thisTransferMap = (TransferMapPtr)( &this->transfermap_DrawPixels);
  57.     ZoomPtr thisZoom = (ZoomPtr)( &this->zoom_DrawPixels);
  58.  
  59.  
  60.     CheckMalloc(this);
  61.     new_RasterPos((RasterPosPtr)this);
  62.     new_Image(thisImage);
  63.     new_TransferMap(thisTransferMap);
  64.     new_Zoom(thisZoom);
  65.     SetDefaults((TestPtr)this, DrawPixelsInfo);
  66.     this->testType = DrawPixelsTest;
  67.     this->subImage = False;
  68.     this->traversalData = 0;
  69.     this->subImageData = 0;
  70.     this->imageData = 0;
  71.     this->usecPixelPrint = " microseconds per pixel with DrawPixels";
  72.     this->ratePixelPrint = " pixels per second with DrawPixels";
  73.     this->usecPrint = " microseconds per image with DrawPixels";
  74.     this->ratePrint = " DrawPixel images per second";
  75.     /* Set virtual functions */
  76.     this->SetState = DrawPixels__SetState;
  77.     this->delete = delete_DrawPixels;
  78.     this->Copy = DrawPixels__Copy;
  79.     this->Initialize = DrawPixels__Initialize;
  80.     this->Cleanup = DrawPixels__Cleanup;
  81.     this->SetExecuteFunc = DrawPixels__SetExecuteFunc;
  82.     this->PixelSize = DrawPixels__Size;
  83.     this->TimesRun = DrawPixels__TimesRun;
  84.     return this;
  85. }
  86.  
  87. void delete_DrawPixels(TestPtr thisTest)
  88. {
  89.     DrawPixelsPtr this = (DrawPixelsPtr)thisTest;
  90.     ImagePtr thisImage = (ImagePtr)(&this->image_DrawPixels);
  91.     TransferMapPtr thisTransferMap = (TransferMapPtr)( &this->transfermap_DrawPixels);
  92.     ZoomPtr thisZoom = (ZoomPtr)( &this->zoom_DrawPixels);
  93.  
  94.  
  95.     delete_Zoom(thisZoom);
  96.     delete_TransferMap(thisTransferMap);
  97.     delete_Image(thisImage);
  98.     delete_RasterPos(thisTest);
  99. }
  100.  
  101. TestPtr DrawPixels__Copy(TestPtr thisTest)
  102. {
  103.     DrawPixelsPtr this = (DrawPixelsPtr)thisTest;
  104.     DrawPixelsPtr newDrawPixels = new_DrawPixels();
  105.     FreeStrings((TestPtr)newDrawPixels);
  106.     *newDrawPixels = *this;
  107.     CopyStrings((TestPtr)newDrawPixels, (TestPtr)this);
  108.     return (TestPtr)newDrawPixels;
  109. }
  110.  
  111. void DrawPixels__Initialize(TestPtr thisTest)
  112. {
  113.     DrawPixelsPtr this = (DrawPixelsPtr)thisTest;
  114.     int windowDim = min(this->environ.windowWidth, this->environ.windowHeight);
  115.     int windowArea = windowDim * windowDim;
  116.     GLint *intTraversalData;
  117.     GLint *srcPtr;
  118.     GLfloat *dstPtr;
  119.     int i;
  120.     const float xFudge = .375;
  121.     const float yFudge = .375;
  122.     float xZoomSgn = (this->zoom_DrawPixels.pixelZoomX >= 0.) ? 1. : -1.;
  123.     float yZoomSgn = (this->zoom_DrawPixels.pixelZoomY >= 0.) ? 1. : -1.;
  124.  
  125.     if ((float)this->image_DrawPixels.imageWidth * fabs(this->zoom_DrawPixels.pixelZoomX) > windowDim)
  126.     this->image_DrawPixels.imageWidth = (float)windowDim/fabs(this->zoom_DrawPixels.pixelZoomX);
  127.     if ((float)this->image_DrawPixels.imageHeight * fabs(this->zoom_DrawPixels.pixelZoomY) > windowDim)
  128.     this->image_DrawPixels.imageHeight = (float)windowDim/fabs(this->zoom_DrawPixels.pixelZoomY);
  129.  
  130.     /* Source image is imageWidth by imageHeight,
  131.      * The portion drawn by glDrawPixels is drawPixelsWidth by drawPixelsHeight */
  132.     if (this->drawPixelsWidth != -1) {
  133.     this->subImage = True;
  134.     } else {
  135.     this->drawPixelsWidth = this->image_DrawPixels.imageWidth;
  136.     }
  137.     if (this->drawPixelsHeight != -1) {
  138.     this->subImage = True;
  139.     } else {
  140.     this->drawPixelsHeight = this->image_DrawPixels.imageHeight;
  141.     }
  142.  
  143.     /* Layout RasterPos coordinates */
  144.     this->numDrawn = 0;
  145.     intTraversalData = CreateSubImageData(windowDim, windowDim, 
  146.                           (float)this->drawPixelsWidth*fabs(this->zoom_DrawPixels.pixelZoomX),
  147.                           (float)this->drawPixelsHeight*fabs(this->zoom_DrawPixels.pixelZoomY),
  148.                           this->acceptObjs, this->rejectObjs, this->clipObjs,
  149.                           this->clipMode, this->clipAmount, this->drawOrder==Spaced, 
  150.                           this->memAlignment, &this->numDrawn);
  151.  
  152.     /* Convert RasterPos coordinates to NDC */
  153.     this->traversalData = (GLfloat*)AlignMalloc(sizeof(GLfloat) * this->rasterPosDim * this->numDrawn, this->memAlignment);
  154.     srcPtr = intTraversalData;
  155.     dstPtr = this->traversalData;
  156.     if (this->zOrder != Coplanar && this->rasterPosDim == 3) {
  157.         GLdouble modelMatrix[16];
  158.         GLdouble projMatrix[16];
  159.         GLint viewport[4];
  160.         GLdouble xd, yd, zd;
  161.     GLdouble depthBits, epsilon;
  162.     GLdouble base, range, delta;
  163.  
  164.         glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  165.         glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  166.         glGetIntegerv(GL_VIEWPORT, viewport);
  167.         glGetDoublev(GL_DEPTH_BITS, &depthBits);
  168.     epsilon = pow(2.0, -depthBits);
  169.         mysrand(15000);
  170.  
  171.     switch (this->zOrder) {
  172.     case Random:
  173.         range = 1. - epsilon;
  174.         base = epsilon;
  175.         delta = 0.;
  176.         break;
  177.     case BackToFront:
  178.         range = (1. - epsilon) / (GLdouble)this->numDrawn;
  179.         base = 1. - range - epsilon;
  180.         delta = -range;
  181.         break;
  182.     case FrontToBack:
  183.         range = (1. - epsilon) / (GLdouble)this->numDrawn;
  184.         base = epsilon;
  185.         delta = range;
  186.         break;
  187.     }
  188.  
  189.     for (i = 0; i < this->numDrawn; i++) {
  190.         /* Perturb the Z value to shake things up */
  191.         GLdouble x = (GLdouble)*srcPtr++ + xFudge;
  192.         GLdouble y = (GLdouble)*srcPtr++ + yFudge;
  193.         GLdouble z = base + 
  194.                          delta * (GLdouble)i +
  195.                          range * (GLdouble)myrand()/(GLdouble)MY_RAND_MAX;
  196.         
  197.         gluUnProject(x, y, z,
  198.                          modelMatrix, projMatrix, viewport,
  199.                          &xd, &yd, &zd);
  200.             *dstPtr++ = (GLfloat)xd * xZoomSgn;
  201.             *dstPtr++ = (GLfloat)yd * yZoomSgn;
  202.             *dstPtr++ = (GLfloat)zd;
  203.         }
  204.     } else {
  205.         for (i = 0; i < this->numDrawn; i++) {
  206.             *dstPtr++ = (((double)(*srcPtr++) + xFudge) / (double)windowDim * 2. - 1.)
  207.                         * xZoomSgn;
  208.             *dstPtr++ = (((double)(*srcPtr++) + yFudge) / (double)windowDim * 2. - 1.)
  209.                         * yZoomSgn;
  210.             if (this->rasterPosDim == 3) *dstPtr++ = -1.;
  211.         }
  212.     }
  213.  
  214.     AlignFree(intTraversalData);
  215.  
  216.     DrawPixels__CreateImageData(this);
  217.  
  218.     /* Layout offsets in source image for subimage draws.
  219.      * All the subimages should lie entirely within the source image, so 100% of 
  220.      * our offsets should be "accepted".  numDrawn taken from RasterPos layout above.
  221.      */
  222.     if (this->subImage) {
  223.     this->subImageData = CreateSubImageData(this->image_DrawPixels.imageWidth, 
  224.                  this->image_DrawPixels.imageHeight,
  225.                  this->drawPixelsWidth,
  226.                  this->drawPixelsHeight,
  227.                                  1., 0., 0., Random, 0., this->drawOrder==Spaced,
  228.                                  this->memAlignment, &this->numDrawn);
  229.     }
  230. }
  231.  
  232. void DrawPixels__Cleanup(TestPtr thisTest)
  233. {
  234.     DrawPixelsPtr this = (DrawPixelsPtr)thisTest;
  235.     int i;
  236.  
  237.     if (this->traversalData) AlignFree(this->traversalData);
  238.     if (this->subImageData) AlignFree(this->subImageData);
  239.     if (this->imageData) {
  240.     void **imageDataPtr = this->imageData;
  241.         for (i = 0; i < this->numObjects; i++) AlignFree(*imageDataPtr++);
  242.     free(this->imageData);
  243.     }
  244.  
  245. }
  246.  
  247. int DrawPixels__SetState(TestPtr thisTest)
  248. {
  249.     DrawPixelsPtr this = (DrawPixelsPtr)thisTest;
  250.  
  251.     ImagePtr thisImage = (ImagePtr)(&this->image_DrawPixels);
  252.     TransferMapPtr thisTransferMap = (TransferMapPtr)( &this->transfermap_DrawPixels);
  253.     ZoomPtr thisZoom = (ZoomPtr)( &this->zoom_DrawPixels);
  254.  
  255.     /* set parent state */
  256.     if (RasterPos__SetState(thisTest) == -1) return -1;
  257.     /* set other inherited classes' states */
  258.     if (Image__SetState(thisImage) == -1) return -1;
  259.     if (TransferMap__SetState(thisTransferMap) == -1) return -1;
  260.     if (Zoom__SetState(thisZoom) == -1) return -1;
  261.  
  262.     /* set own state */
  263.  
  264.     return 0;
  265. }
  266.  
  267. void DrawPixels__SetExecuteFunc(TestPtr thisTest)
  268. {
  269.     DrawPixelsPtr this = (DrawPixelsPtr)thisTest;
  270.     DrawPixelsFunc function;
  271.  
  272.     function.word = 0;
  273.  
  274.     function.bits.functionPtrs = this->loopFuncPtrs;
  275.     function.bits.subimage = this->subImage;
  276.     function.bits.multiimage = (this->numObjects > 1);
  277.  
  278.     /* Dimensions of data to be traversed */
  279.     function.bits.rasterPosDim  = this->rasterPosDim - 2;
  280.  
  281.     this->Execute = DrawPixelsExecuteTable[function.word];
  282. }
  283.  
  284. int DrawPixels__TimesRun(TestPtr thisTest)
  285. {
  286.     DrawPixelsPtr this = (DrawPixelsPtr)thisTest;
  287.  
  288.     return this->numDrawn;
  289. }
  290.  
  291. float DrawPixels__Size(TestPtr thisTest)
  292. {
  293.     DrawPixelsPtr this = (DrawPixelsPtr)thisTest;
  294.     int accept = this->acceptObjs * (float)(this->numDrawn);
  295.     int reject = this->rejectObjs * (float)(this->numDrawn);
  296.     int clip = this->clipObjs * (float)(this->numDrawn);
  297.     float size = (float)this->drawPixelsWidth * fabs(this->zoom_DrawPixels.pixelZoomX) * 
  298.                  (float)this->drawPixelsHeight * fabs(this->zoom_DrawPixels.pixelZoomY);
  299.     float acceptSize, clipSize;
  300.  
  301.     accept += this->numDrawn - accept - reject - clip;
  302.  
  303.     acceptSize = (float)accept * size;
  304.     clipSize = (float)clip * (1. - this->clipAmount) * size;
  305.  
  306.     return (acceptSize + clipSize)/(float)this->numDrawn;
  307. }
  308.  
  309. void DrawPixels__CreateImageData(DrawPixelsPtr this)
  310. {
  311.     int i;
  312.     void **imagePtr;
  313.     int imageSize;
  314.     void *image;
  315.  
  316.  
  317.     this->imageData = (void**)malloc(sizeof(void*) * this->numObjects);
  318.     CheckMalloc(this->imageData);
  319.     imagePtr = this->imageData;
  320.     if (this->numObjects)
  321.     image = new_ImageData(
  322.                          this->image_DrawPixels.imageWidth,
  323.              this->image_DrawPixels.imageHeight,
  324.              this->image_DrawPixels.imageFormat,
  325.              this->image_DrawPixels.imageType,
  326.              this->image_DrawPixels.imageAlignment,
  327.              this->image_DrawPixels.imageSwapBytes,
  328.              this->image_DrawPixels.imageLSBFirst,
  329.              this->memAlignment,
  330.              &imageSize);
  331.     *imagePtr++ = image;
  332.     for (i = 1; i < this->numObjects; i++) {
  333.     *imagePtr = AlignMalloc(imageSize, this->memAlignment);
  334.     memcpy(*imagePtr, image, imageSize);
  335.     imagePtr++;
  336.     }
  337. }
  338.